package com.crm.service.saling.impl;

import com.crm.dao.saling.CODetailsDao;
import com.crm.dao.saling.ContractDao;
import com.crm.exception.ServiceException;
import com.crm.pojo.*;
import com.crm.service.saling.CODetailsService;
import com.crm.vo.CODetailsVO;
import com.crm.vo.ContractClientCodetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dr5hx on 2018/8/9
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CODetailsServiceImpl implements CODetailsService {
    @Autowired

    private CODetailsDao coDetailsDao;
    @Autowired
    private ContractDao contractDao;

    /**
     * 删除订单/合同详情
     *
     * @param contractId
     */
    @Override
    public void deleteCODetailsById(Long contractId) {
        coDetailsDao.deleteCODetailsById(new BigDecimal(contractId));
    }

    /**
     * 保存或者修改合同细节
     *
     * @param coDetail
     * @param id
     * @throws ServiceException
     */
    @Override
    public void saveCODetail(CODetail coDetail, Long id) throws ServiceException {
        try {
            Contract c = contractDao.findOne(id);
            coDetail.setContract(c);
            coDetailsDao.save(coDetail);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 查询所有的订单数据细节
     *
     * @return
     */
    @Override
    public List<ContractClientCodetailsVO> getContractClientCodetailsVOList(int currpage, int rows) throws ServiceException {
        try {
            PageRequest pageRequest = new PageRequest(currpage - 1, rows);
            return coDetailsDao.getContractClientCodetailsVOList(pageRequest);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 显示所有的客户的计划回款记录
     *
     * @param clientId
     * @return
     * @throws ServiceException
     */
    @Override
    public List<PlaningBack> getPlaningBackRecordListByClientId(Long clientId) throws ServiceException {
        try {
            return coDetailsDao.getPlaningBackRecordListByClientId(new BigDecimal(clientId.toString()));

        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 显示所有的回款记录
     *
     * @param clientId
     * @return
     * @throws ServiceException
     */
    @Override
    public List<Back> getBackRecordListByClientId(Long clientId) throws ServiceException {
        try {
            return coDetailsDao.getBackRecordListByClientId(new BigDecimal(clientId.toString()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }

    }

    /**
     * 根据客户编号查询发货单
     *
     * @param clientId
     * @return
     * @throws ServiceException
     */
    @Override
    public List<Dispatcher> getDispatcherRecordListByClientId(Long clientId) throws ServiceException {
        try {
            return coDetailsDao.getDispatcherRecordListByClientId(new BigDecimal(clientId.toString()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 查询对应客户的对应的发货信息
     *
     * @param clientId
     * @return
     * @throws ServiceException
     */
    @Override
    public List<DispatcherDetail> getDispatcherDetailRecordListByClientId(Long clientId) throws ServiceException {
        try {
            return coDetailsDao.getDispatcherDetailRecordListByClientId(new BigDecimal(clientId.toString()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 根据客户编号查询该客户得所有订单，以及每个订单对应的订单详情
     *
     * @param clientId
     * @return
     * @throws ServiceException
     */
    @Override
    public List<CODetailsVO> getAllCODetailVOByClientId(Long clientId) throws ServiceException {
        try {
            return coDetailsDao.getAllCODetailVOByClientId(new BigDecimal(clientId));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 获取对应合同的所有money
     *
     * @param contractId
     * @return
     * @throws ServiceException
     */
    @Override
    public BigDecimal getCodetailMoneyByContractId(Long contractId) throws ServiceException {
        try {
            Contract contract = contractDao.findOne(contractId);
            contract.setContractMoney(coDetailsDao.getCodetailMoneyByContractId(contract));
            return coDetailsDao.getCodetailMoneyByContractId(contract);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    @Override
    public List<CODetail> saveCoDetailList(List<CODetail> list) throws ServiceException {
        try {
            Iterable iterator = coDetailsDao.save(list);
            List<CODetail> coDetailList = new ArrayList<>();
            iterator.forEach(e -> coDetailList.add((CODetail) e));
            return coDetailList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 显示对应的订单/合同的订单详情/合同详情
     *
     * @param orderId
     * @return
     */
    @Override
    public List<CODetailsVO> showOrderDetailsList(Long orderId) {
        Contract contract = contractDao.findOne(orderId);
        return coDetailsDao.showOrderDetailsList(contract);
    }

    /**
     * @param coDetail
     * @return
     */
    @Override
    public CODetail saveEditCODetail(CODetail coDetail) {
        return coDetailsDao.save(coDetail);
    }

    /**
     * 获取一个合同详细信息
     *
     * @param coId
     * @return
     */
    @Override
    public CODetailsVO getOneCODetail(Long coId) {
        return coDetailsDao.getOneCODetail(coId);
    }
}
